/*
 * Copyright (C) 2004, Velocityme Partnership
 * GroupActions.java
 *
 * Created on 09 October 2004, 12:23
 */

package com.velocityme.www.interfaces.nodeactions;

import com.velocityme.entity.Group;
import com.velocityme.entity.User;
import com.velocityme.session.GroupLocal;
import com.velocityme.session.KeyLocal;
import com.velocityme.session.NodeLocal;
import com.velocityme.www.actionforms.nodeactionforms.GroupActionForm;
import com.velocityme.www.dto.IdName;
import com.velocityme.www.utility.NodeUtil;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author  Robert
 */
public class GroupActions extends NodeActions {

    /** Creates a new instance of GroupActions */
    public GroupActions() {
    }

    @Override
    public ActionForward show(ActionMapping mapping,
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        Long nodeId = (Long)request.getAttribute("nodeId");
        HttpSession session = request.getSession();
        KeyLocal key = (KeyLocal)session.getAttribute("sessionKey");
        session.setAttribute("group", lookupGroupBean().findGroup(key, nodeId));
        return mapping.findForward("ACTION_DISPLAY");
    }

    @Override
    public ActionForward showCreate(ActionMapping mapping,
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response) throws Exception {
        Long nodeId = (Long)request.getAttribute("nodeId");
        HttpSession session = request.getSession();
        KeyLocal key = (KeyLocal)session.getAttribute("sessionKey");
        session.setAttribute("unassignedUserPairs", NodeUtil.getAllUserPairs(key));
        session.setAttribute("assignedUserPairs", new ArrayList<IdName>());
        Group group = new Group();
        group.setParentNode(lookupNodeBean().findNode(key, nodeId));
        session.setAttribute("group", group);
        return mapping.findForward("ACTION_EDIT");
    }

    @Override
    public ActionForward showEdit(ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response) throws Exception {
        Long nodeId = (Long)request.getAttribute("nodeId");
        HttpSession session = request.getSession();
        KeyLocal key = (KeyLocal)session.getAttribute("sessionKey");
        Group group = lookupGroupBean().findGroup(key, nodeId);
        session.setAttribute("group", group);
        Collection<IdName> assignedUserPairs = new ArrayList<IdName>();
        for (User user: group.getUsers()) {
            assignedUserPairs.add(new IdName(user.getId(), user.getName()));
        }
        Collection<IdName> allUserPairs = NodeUtil.getAllUserPairs(key);
        allUserPairs.removeAll(assignedUserPairs);
        session.setAttribute("unassignedUserPairs", allUserPairs);
        session.setAttribute("assignedUserPairs", assignedUserPairs);
        return mapping.findForward("ACTION_EDIT");
    }

    @Override
    public ActionForward edit(ActionMapping mapping,
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeyLocal key = (KeyLocal)session.getAttribute("sessionKey");
        Long nodeId;
        if (request.getParameter("btnSubmit").equals("OK")) {
            Group group = (Group)session.getAttribute("group");
            ((GroupActionForm)form).extractGroup(key, group);
            if (group.getId() != null) {
                lookupGroupBean().editGroup(key, group);
            }
            else {
                group = lookupGroupBean().createGroup(key, group);
            }
            nodeId = group.getId();
        }
        // Cancel simply displays the original node again
        else {
            nodeId = (Long)session.getAttribute("nodeId");
        }
        request.setAttribute("nodeId", nodeId);
        return mapping.findForward("ACTION_SUCCESS");
    }

    private GroupLocal lookupGroupBean() {
        try {
            Context c = new InitialContext();
            return (GroupLocal) c.lookup("java:comp/env/GroupBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private NodeLocal lookupNodeBean() {
        try {
            Context c = new InitialContext();
            return (NodeLocal) c.lookup("java:comp/env/NodeBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

}
